package org.wkg;

import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Demo03_Statment {

    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java114?characterEncoding=utf8" +
                    "&allowPublicKeyRetrieval=true&useSSL=false ", "root", "123456");
            statement = connection.createStatement();
            Scanner scanner = new Scanner(System.in);
            String InName = scanner.next();
            String sql = "select id,name,age,gender from student where name='" + InName + "'";
            resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                long id = resultSet.getLong(1);
                String name= resultSet.getString(2);
                int age = resultSet.getInt(3);
                byte gender = resultSet.getByte(4);
                System.out.println(MessageFormat.format("学生编号={0}, 学生姓名={1}, 学生年龄={2}, 学生性别={3}",
                        id,name,age,gender));
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
